import { createStore, combineReducers, applyMiddleware, compose } from 'redux';
import ReduxThunk from 'redux-thunk';
import ReduxPromise from 'redux-promise';
import { persistStore, persistReducer } from 'redux-persist';
import storage from 'redux-persist/lib/storage'; // defaults to localStorage for web

import CityReducer from './reducer/CityReducer';
import CinemaReducer from './reducer/CinemaReducer';
import FilmReducer from './reducer/FilmReducer';
import TabbarReducer from './reducer/TabbarReducer';

const persistConfig = {
  key: 'myRoot',
  storage,
  whitelist: ['CityReducer'], // only navigation will be persisted
};

const reducer = combineReducers({
  CityReducer,
  TabbarReducer,
  CinemaReducer,
  FilmReducer,
});

const persistedReducer = persistReducer(persistConfig, reducer);

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const store = createStore(
  persistedReducer,
  composeEnhancers(applyMiddleware(ReduxThunk, ReduxPromise))
);

const persistor = persistStore(store);

export { store, persistor };
